package com.bawei.module_live.ui.dialog

import android.graphics.Rect
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_live.databinding.DialogRedPacketBinding
import com.bawei.module_live.ui.dialog.adapter.LuckListAdapter
import com.bawei.module_live.ui.dialog.model.LuckUser

class RedPacketDialogFragment : DialogFragment() {

    private var _binding: DialogRedPacketBinding? = null
    private val binding get() = _binding!!

    enum class State { COUNTDOWN, GRAB, RESULT, LUCK_LIST }
    private var state: State = State.COUNTDOWN
    private var countdown = 3
    private val handler = Handler(Looper.getMainLooper())

    interface RedPacketListener {
        fun onGrabbed()
    }
    var listener: RedPacketListener? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = DialogRedPacketBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCanceledOnTouchOutside(true)
        binding.ivClose.setOnClickListener { dismiss() }
        binding.rvLuckList.layoutManager = LinearLayoutManager(context)
        binding.rvLuckList.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
            ) {
                outRect.top = 8
                outRect.bottom = 8
            }
        })
        binding.btnGrab.isEnabled = true
        binding.btnGrab.visibility = View.VISIBLE
        binding.tvRedPacketTip.text = "点击下方按钮抢红包"
        binding.tvRedPacketTip.visibility = View.VISIBLE
        binding.tvResult.visibility = View.GONE
        binding.rvLuckList.visibility = View.GONE
        binding.btnGrab.setOnClickListener { grabRedPacket() }
    }

    private fun grabRedPacket() {
        state = State.RESULT
        binding.btnGrab.isEnabled = false
        binding.tvRedPacketTip.text = "正在抢红包..."
        handler.postDelayed({
            binding.tvRedPacketTip.visibility = View.GONE
            binding.btnGrab.visibility = View.GONE
            binding.tvResult.visibility = View.VISIBLE
            binding.tvResult.text = "恭喜你抢到 8.88 元"
            listener?.onGrabbed()
            showLuckList()
        }, 1500)
    }

    private fun showLuckList() {
        binding.tvResult.visibility = View.GONE
        binding.rvLuckList.visibility = View.VISIBLE
        val list = listOf(
            LuckUser("ic_avatar_placeholder", "小明", 8.88),
            LuckUser("ic_avatar_placeholder", "小红", 6.66),
            LuckUser("ic_avatar_placeholder", "小刚", 5.20),
            LuckUser("ic_avatar_placeholder", "小美", 3.33),
            LuckUser("ic_avatar_placeholder", "小强", 2.00)
        )
        binding.rvLuckList.adapter = LuckListAdapter(list)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        handler.removeCallbacksAndMessages(null)
        _binding = null
    }
} 